import Menus from './menus'
import {flatMapDeep, find} from "lodash";
export default function ({ app, store }) {
  app.router.beforeEach((to, from ,next) => {
    const { path, fullPath } = to
    if (fullPath === '/login') {
      next();
    } else {
      const pathArr = path.slice(1).split('/')
      console.log('pathArr', pathArr);
      const flatMenus = flatMapDeep(Menus, item => item.menus)
      const currentMenu = find(flatMenus, { id: pathArr[0] || 'home' })
      // console.log('currentMenu', currentMenu)
      let activeMenu = ''
      if (currentMenu.children) {
        const target = find(currentMenu.children, { path });
        activeMenu = target ? target.title : '';
      }
      store.dispatch('menus/setPageMenu', {
        menus: Menus[currentMenu.module].menus,
        openSubmenus: [currentMenu.title],
        activeMenu,
        moduleName: currentMenu.module
      }).then(() => {
        console.log('next');
        next()
      })
    }
  })
}
